
import { _decorator, Component, Node, Camera, CameraComponent, systemEvent, SystemEvent, EventKeyboard, KeyCode } from 'cc';
const { ccclass, property } = _decorator;
 
@ccclass('SwitchCamera')
export class SwitchCamera extends Component {

    @property({type:[CameraComponent]})
    cameraList:Camera[]=[]

    onLoad(){
        systemEvent.on(SystemEvent.EventType.KEY_DOWN,this.keyDown,this);

        let r=Math.random()*this.cameraList.length;
        this.switchCamera(r|0);
        //this.switchCamera(0);
    }

    switchCamera(type:number){
        if(type<0||type>this.cameraList.length){
            return;
        }
        for(let camera of this.cameraList){
            if(camera){
                camera.enabled=false;
            }
        }
        this.cameraList[type].enabled=true;
    }

    i:number=0;
    keyDown(e:EventKeyboard){
        if(e.keyCode!=KeyCode.SPACE){
            return; 
        }
        this.switchCamera(this.i);
        this.i=(this.i+1)%this.cameraList.length;
    }
}
